package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 霍森彪
 * @date 2022/7/26 17:53
 */
@Service
@Slf4j
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {

    @Autowired
    private ApUserRealnameMapper apUserRealnameMapper;

    @Autowired
    private ApUserMapper apUserMapper;

    @Autowired
    private ApUserRealnameMapper apUserRealNameMapper;

    @Autowired
    private IWemediaClient  iWemediaClient;

    @Override
    public ResponseResult authList(AuthDto dto) {
        //判空
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE, "前端传入的参数为空");
        }
        //获取前端传入的参数
        Integer pageNum = dto.getPage();
        Integer pageSize = dto.getSize();
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 10 : pageSize;
        Short status = dto.getStatus();

        //分页查询
        Page<ApUserRealname> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ApUserRealname> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(status != null, ApUserRealname::getStatus, status);
        Page<ApUserRealname> selectPage = apUserRealnameMapper.selectPage(page, wrapper);

        //构建返回对象
        Long total = selectPage.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(pageNum, pageSize, total.intValue());
        pageResponseResult.setData(selectPage.getRecords());
        return pageResponseResult;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public ResponseResult auth(AuthDto dto, Integer type) {
        // 1.入参检测
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        // 2.提取id
        Integer id = dto.getId();

        // 3.根据id查询认证记录
        ApUserRealname apUserRealname = apUserRealNameMapper.selectById(id);
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        // 4.更新认证审核状态
        apUserRealname.setStatus((short) (type == 1 ? 9 : 2));
        apUserRealname.setId(id);
        apUserRealname.setReason(dto.getMsg());
        apUserRealNameMapper.updateById(apUserRealname);

        // 5.通过
        if (type == 1) {
            // 5.1 通过认证记录获得对应的用户名
            String name = apUserRealname.getName();

            // 5.2 调用Feign接口完成媒体账号创建
            Map<String, String> map = new HashMap<>();
            map.put("name", name);

            ResponseResult responseResult = iWemediaClient.addUser(map);
            if (responseResult == null || responseResult.getCode() != 200) {
                throw new RuntimeException("自媒体用户创建失败");
            }

            // 5.3 更新APP用户的flag状态
            ApUser apUser = new ApUser();
            apUser.setId(apUserRealname.getUserId());
            apUser.setFlag((short) 1);
            int updateResult = apUserMapper.updateById(apUser);
            if (updateResult < 1) {
                throw new RuntimeException("APP用户状态更新失败");
            }
        }

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
